package com.portal.back.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.portal.back.service.ISysProfessionalService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysProfessional;

/**
 * @Title:行业视点service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-14
 * 
 */
public class SysProfessionalService implements ISysProfessionalService {
	private IBaseDao<SysProfessional, Integer> dao;

	public SysProfessionalService() {
		dao = new BaseDao<SysProfessional, Integer>(SysProfessional.class);
	}

	public boolean addProfession(SysProfessional professional) {
		String sql = "";
		try {
			if (professional.getId() == null) {
				sql = "insert into sys_professional(title,descrition,ordNum) values(?,?,?) ";
				dao.insert(sql, new Object[] { professional.getTitle(),
						professional.getDescribe(), professional.getCode() });
			} else {
				sql = "update sys_professional set title=?,descrition=?,ordNum=?  where id=?";
				dao.update(sql, new Object[] { professional.getTitle(),
						professional.getDescribe(), professional.getCode(),
						professional.getId() });
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<SysProfessional> getAllPro() {
		String sql = "select * from sys_professional order by ordNum ";
		List<SysProfessional> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysProfessional> newlist = new ArrayList<SysProfessional>();
			for (int i = 0; i < list.size(); i++) {
				SysProfessional pro = list.get(i);

				String content = pro.getDescribe();
				if (content != null && !"".equals(content)
						&& content.length() > 20)
					pro.setDescribe(content.substring(0, 40) + "...");
				newlist.add(pro);
			}
			return newlist;
		}
		return null;
	}

	public boolean deleteById(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "delete from sys_professional where  id=?";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_professional where  id=" + delteid[0];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public SysProfessional getById(String id) {
		String sql = "select * from sys_professional where  id=?";
		List<SysProfessional> list = dao.findList(sql, new Object[] { id });
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public List<SysProfessional> find(int start, int limit) {
		String sql = "select * from sys_professional order by ordNum";
		if (start >= 0 && limit > 0)
			sql = dao.createSqlQuery(sql).setFirstResult(start).setMaxSize(
					limit).bulidSql();
		return dao.findList(sql);
	}
}
